package com.jiashihui.schema_lgorithm.spring;

import com.jiashihui.schema_lgorithm.spring.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author jiashihui
 * @describe: 创建JavaBean对象的三种方法
 * @date 2019/03/20
 */
public class CreateJavaBean {

    //  <!-- 使用构造函数创建JavaBean对象 -->
    @Test
    public void getBean1() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        User bean = context.getBean("user", User.class);
        bean.eat();
        // 摧毁studentService实例对象
        // Close 关闭 才可执行 destroy-method
        ((ClassPathXmlApplicationContext) context).close();
    }

    // <!-- 使用静态工厂创建JavaBean对象 -->
    @Test
    public void getBean2() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        User bean = context.getBean("userStaticFactory", User.class);
        bean.eat();
    }

    // <!-- 使用动态工厂创建JavaBean对象象 -->
    @Test
    public void getBean3() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        User bean = context.getBean("userDynamicFactory", User.class);
        bean.eat();
    }
}
